package cn.stylefeng.guns.modular.api.controller;

import cn.stylefeng.guns.core.util.UserUtil;
import cn.stylefeng.guns.sys.core.exception.enums.BizExceptionEnum;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.mapper.UserMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.UserParam;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.reqres.response.SuccessResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import cn.stylefeng.roses.kernel.model.exception.RequestEmptyException;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/api/user")
@Api(tags = "用户管理", description = "用户管理")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserMapper userMapper;
    /**
     * 获取用户详情
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:43
     */
    @PostMapping("/getUserInfo")
    @ResponseBody
    @ApiOperation(value = "详情接口", notes = "详情", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "详情", response = ResponseData.class)})
    public SuccessResponseData getUserInfo(HttpServletRequest request) throws ServiceException{


        String userId =  UserUtil.getUserIdFromToken(request);
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.NOT_LOGIN.getCode(),"请重新登录");
        }
        Long userIdLong = Long.valueOf(userId);
//        this.userService.assertAuth(userIdLong);
        return new SuccessResponseData(userService.getUserInfo(userIdLong));
    }

    @PostMapping("/allList")
    @ResponseBody
    @ApiOperation(value = "列表接口", notes = "列表", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "列表", response = ResponseData.class)})
    public ResponseData allList(UserParam userParam) {
        User user = new User();
        BeanUtils.copyProperties(userParam, user);
        List<User> users = userService.selectUserList(user);
        return ResponseData.success(users);
    }
    /**
     * 获取用户详情
     *
     * @author fengshuonan
     * @Date 2018/12/24 22:43
     */
    @PostMapping("/saveCode")
    @ResponseBody
    @ApiOperation(value = "存储code接口", notes = "存储code接口", response = ResponseData.class)
    @ApiResponses({@ApiResponse(code = 200, message = "存储code接口", response = ResponseData.class)})
    public ResponseData saveCode(HttpServletRequest request,UserParam userParam) throws ServiceException{


        String userId =  UserUtil.getUserIdFromToken(request);
        if (ToolUtil.isEmpty(userId)) {
            throw new ServiceException(BizExceptionEnum.NOT_LOGIN.getCode(),"请重新登录");
        }
        Long userIdLong = Long.valueOf(userId);

        User user = new User();
        user.setUserId(userIdLong);
        user.setWxcode(userParam.getWxcode());
        return new SuccessResponseData(userMapper.updateById(user));
    }
}
